<template>
    <view class="content">
        <hx-navbar :config="config" />
        <view class="order-info">
            <view class="order-info-boxs">
                <view class="order-info-box">
                    <view class="order-info-label">订单类型：</view>
                    <view class="order-info-cont">{{orderInfo.order_type == 1?"正式订单":"预下订单"}}</view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">订单编号：</view>
                    <view class="order-info-cont">{{orderInfo.order_number}}</view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">合同文本：</view>
                    <view class="order-info-cont order-path"
                        v-if="orderInfo && orderInfo.contract_url && orderInfo.contract_url.path"
                        @click="previewFile(orderInfo.contract_url.path)">
                        <!-- 可点击的文本，触发预览PDF -->
                        <text>
                            {{ orderInfo.contract_url.tag}}
                        </text>
                        <!-- 可点击的文本，触发预览PDF -->
                        <text class="pdf-link">
                            预览
                        </text>
                    </view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">技术协议：</view>
                    <view class="order-info-cont order-path"
                        v-if="orderInfo && orderInfo.technical_agreement_url && orderInfo.technical_agreement_url.path"
                        @click="previewFile(orderInfo.technical_agreement_url.path)">
                        <!-- 可点击的文本，触发预览PDF -->
                        <text>
                            {{ orderInfo.technical_agreement_url.tag}}
                        </text>
                        <!-- 可点击的文本，触发预览PDF -->
                        <text class="pdf-link">
                            预览
                        </text>
                    </view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">合同签订：</view>
                    <view class="order-info-cont">{{orderInfo.signing_name}}</view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">客户名称：</view>
                    <view class="order-info-cont">{{orderInfo.customer_name}}</view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">
                        <text>客户合同</text>
                        <text>编号：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.customer_contract_number}}</view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">
                        <text>客户所在</text>
                        <text>区域：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.province}}{{orderInfo.city}}{{orderInfo.area}}</view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">
                        <text>签订时间：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.signing_time}}</view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">
                        <text>交付时间：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.delivery_time}}</view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">
                        <text>交付内容：</text>
                    </view>
                    <view class="order-info-cont" @click="orderProductView(orderInfo.id)">
                        <view class="pdf-link">查看交付内容</view>
                    </view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">
                        <text>合同金额：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.contract_amount}}</view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">
                        <text>收货人：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.consignee}}</view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">
                        <text>收货人手</text>
                        <text>机号：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.consignee_mobile}}</view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">
                        <text>收货地址：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.delivery_address}}</view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">
                        <text>是否施工：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.is_construction == 1?"是":"否"}}</view>
                </view>
                <view class="order-info-box" v-if="orderInfo.is_construction == 1">
                    <view class="order-info-label">
                        <text>施工资料：</text>
                    </view>
                    <view class="order-info-cont order-path" v-if="orderInfo && orderInfo.construction_document">
                        <view class="order-file" v-for="(item,index) in orderInfo.construction_document" :key="index"
                            @click="previewFile(item.path)">
                            <text>
                                {{ item.tag}}
                            </text>
                            <text class="pdf-link">
                                预览
                            </text>
                        </view>
                    </view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">
                        <text>生产类型：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.product_production_type == 1?"自产":"第三方采购"}}</view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">
                        <text>设计人员：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.designer_name}}</view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">
                        <text>设计复审：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.review_design_name}}</view>
                </view>
                <view class="order-info-box order-info-box-center">
                    <view class="order-info-label">
                        <text>清单图纸</text>
                        <text>提交完成：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.sub_data==1?"是":"否"}}</view>
                </view>
                <view class="order-info-box order-info-box-center">
                    <view class="order-info-label">
                        <text>采购资料</text>
                        <text>提交完成：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.sub_procurement_data==1?"是":"否"}}</view>
                </view>
                <view class="order-info-box order-info-box-center">
                    <view class="order-info-label">
                        <text>物料入库</text>
                        <text>提交完成：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.sub_material_warehousing_data==1?"是":"否"}}</view>
                </view>
                <view class="order-info-box order-info-box-center">
                    <view class="order-info-label">
                        <text>预计生产</text>
                        <text>完成时间：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.estimated_completion_time}}</view>
                </view>

                <view class="order-info-box order-info-box-center">
                    <view class="order-info-label">
                        <text>报检资料</text>
                        <text>提交完成：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.sub_quality_inspection_data==1?"是":"否"}}</view>
                </view>
                <view class="order-info-box order-info-box-center">
                    <view class="order-info-label">
                        <text>质检资料</text>
                        <text>提交完成：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.sub_qualified_qualified_data==1?"是":"否"}}</view>
                </view>
                <view class="order-info-box order-info-box-center">
                    <view class="order-info-label">
                        <text>发货资料</text>
                        <text>提交完成：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.sub_shipment_data==1?"是":"否"}}</view>
                </view>
                <view class="order-info-box order-info-box-center">
                    <view class="order-info-label">
                        <text>回单资料</text>
                        <text>提交完成：</text>
                    </view>
                    <view class="order-info-cont">{{orderInfo.sub_shipment_receipt_data==1?"是":"否"}}</view>
                </view>
                <view class="order-info-box">
                    <view class="order-info-label">
                        <text>回单图片：</text>
                    </view>
                    <view class="order-info-cont" v-if="orderInfo && orderInfo.shipment_receipt_image">
                        <view class="order-image" v-for="(item,index) in orderInfo.shipment_receipt_image" :key="index"
                            @click="previewFile(item.path)">
                            <image :src="item.path"></image>
                        </view>
                    </view>
                </view>
            </view>
        </view>
    </view>
    </view>
</template>

<script>
    import {
        detail
    } from '@/common/api/order.js'
    export default {
        data() {
            return {
                config: {
                    back: true,
                    title: '订单详情',
                    fontSize: '16px',
                    color: '#ffffff',
                    //背景颜色;参数一：透明度（0-1）;参数二：背景颜色（array则为线性渐变，string为单色背景）
                    backgroundColor: [1, ['#6970ff', '#6970ff']],
                    // 状态栏 ，数组则为滑动变色
                    statusBarBackground: ['', '#ffffff'],
                },
                orderInfo: []
            }
        },
        onLoad(options) {
            this.orderDetail(options.id)
        },
        methods: {
            orderProductView(id) {
                uni.navigateTo({
                    url: "/pages/index/product?id=" + id + "&type=detail",
                })
            },

            orderDetail(id) {
                detail({
                    id: id
                }).then((res) => {
                    this.orderInfo = res
                }).catch((err) => {

                });
            },
            // 从URL中提取文件类型（根据后缀名）
            getFileTypeFromUrl(url) {
                if (!url) return null;
                // 匹配URL中最后一个"."后的字符串（忽略参数）
                const match = url.match(/\.([a-zA-Z0-9]+)(\?.*)?$/);
                if (match && match[1]) {
                    const ext = match[1].toLowerCase(); // 转为小写（兼容大小写后缀，如.PDF/.XLSX）
                    // 只保留我们需要的类型（pdf/xlsx/xls）
                    if (['pdf', 'xlsx', 'xls', 'jpg', 'png', 'jpeg'].includes(ext)) {
                        return ext;
                    }
                }
                return null; // 无法识别的类型
            },

            // 通用文件预览（基于URL后缀判断类型）
            previewFile(fileUrl) {
                if (!fileUrl) {
                    uni.showToast({
                        title: '暂无文件',
                        icon: 'none'
                    });
                    return;
                }

                // 1. 通过 URL 获取文件类型
                const fileType = this.getFileTypeFromUrl(fileUrl);
                if (!fileType) {
                    uni.showToast({
                        title: '不支持的文件类型',
                        icon: 'none'
                    });
                    return;
                }

                // 区分文件类型处理
                if (['png', 'jpg', 'jpeg'].includes(fileType)) {
                    // 图片类型，使用 uni.previewImage 预览
                    uni.previewImage({
                        urls: [fileUrl], // 图片预览接口要求传入数组，这里单个图片也用数组形式
                        success: (res) => {
                            console.log(`${fileType} 预览成功`, res);
                        },
                        fail: (err) => {
                            console.error(`${fileType} 预览失败`, err);
                            uni.showToast({
                                title: `打开图片失败`,
                                icon: 'none'
                            });
                        }
                    });
                } else {
                    // 文档类型，走原来的下载 + 打开流程
                    // 2. 下载文件（与之前逻辑一致）
                    uni.downloadFile({
                        url: fileUrl,
                        success: (downloadRes) => {
                            if (downloadRes.statusCode === 200) {
                                // 3. 打开文件（使用解析出的类型）
                                uni.openDocument({
                                    filePath: downloadRes.tempFilePath,
                                    fileType: fileType,
                                    showMenu: true,
                                    success: () => {
                                        console.log(`${fileType} 打开成功`);
                                    },
                                    fail: (err) => {
                                        console.error(`打开${fileType}失败`, err);
                                        uni.showToast({
                                            title: `打开文件失败`,
                                            icon: 'none'
                                        });
                                    }
                                });
                            } else {
                                uni.showToast({
                                    title: '文件下载失败',
                                    icon: 'none'
                                });
                            }
                        },
                        fail: () => {
                            uni.showToast({
                                title: '网络请求失败',
                                icon: 'none'
                            });
                        }
                    });
                }
            }
        }
    }
</script>

<style>
    page {
        background: #f5f5f5;
    }

    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }

    .order-info {

        width: 100%;
        margin-top: 20rpx;

    }

    .order-info-boxs {
        display: flex;
        flex-direction: column;
        background-color: #fff;
        padding: 10rpx;
        border-radius: 10rpx;
        margin: 0 20rpx 15rpx 20rpx;
    }

    .order-info-box {
        display: flex;
        font-size: 23rpx;
        align-items: flex-start;
        margin-bottom: 15rpx;
    }

    .order-info-box-center {
        align-items: center;
    }

    .order-info-label {
        font-weight: bold;
        width: 18%;
        display: flex;
        flex-direction: column;
    }

    .order-info-cont {
        width: 80%;
        display: flex;
        flex-wrap: wrap
    }

    .pdf-link {
        color: #6970ff;
        text-decoration: underline;
    }

    .order-path {
        display: flex;
        flex-direction: column;
    }

    .order-path text:first-of-type {
        width: 100%;
        flex-wrap: wrap;
    }

    .order-file {
        display: flex;
        flex-direction: column;
        margin-bottom: 10rpx;
    }

    .order-file text:first-of-type {
        width: 100%;
        flex-wrap: wrap;
    }

    .order-image {
        margin: 0 10rpx 10rpx 0;
    }

    .order-image image {
        width: 100rpx;
        height: 100rpx;
    }
</style>